package top.gradual.springcloud.component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

/**
 * @Description: 回退
 * @Author: gradual
 * @Date: 2019-06-28 15:44
 */
@Slf4j
@Component
public class ServiceConsumerFallbackProvider implements ZuulFallbackProvider {

    /**
     * @description: 返回*对所有服务进行回退操作
     *
     * @date: 2019/6/28 15:56
     * @author: gradual
     *
     * @return: java.lang.String
     */
    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
            /**
             * @description: 设置返回请求头参数
             *
             * @date: 2019/6/28 15:58
             * @author: gradual
             *
             * @return: org.springframework.http.HttpHeaders
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders httpHeaders = new HttpHeaders();
                MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
                httpHeaders.setContentType(mediaType);
                return httpHeaders;
            }

            /**
             * @description: 返回回退的内容
             *
             * @date: 2019/6/28 15:58
             * @author: gradual
             *
             * @return: java.io.InputStream
             */
            @Override
            public InputStream getBody() throws IOException {
                RequestContext requestContext = RequestContext.getCurrentContext();
                Throwable throwable = requestContext.getThrowable();
                if (throwable != null) {
                    log.error("", throwable.getCause());
                }
                Map<String, Object> responseData = new HashMap<>();
                responseData.put("code", 700);
                responseData.put("message", "服务器内部错误");
                responseData.put("data", null);
                return new ByteArrayInputStream(new ObjectMapper().writeValueAsBytes(responseData));
            }

            /**
             * @description: 返回响应的状态码
             *
             * @date: 2019/6/28 15:57
             * @author: gradual
             *
             * @return: org.springframework.http.HttpStatus
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }


            @Override
            public int getRawStatusCode() throws IOException {
                return this.getStatusCode().value();
            }

            /**
             * @description: 返回响应码对应文本
             *
             * @date: 2019/6/28 15:57
             * @author: gradual
             *
             * @return: java.lang.String
             */
            @Override
            public String getStatusText() throws IOException {
                return this.getStatusCode().getReasonPhrase();
            }

            @Override
            public void close() {

            }
        };
    }
}
